/**
 * @file verRecetaDetallado.java
 * @brief Interfaz que muestra las líneas de recetas para la receta elegida en verReceta
 *
 * Copyright (C)  - May [2011]
 *
 * @author Miguel Ángel Díaz López 
 * @version 01.00
 *
 * @footnote e-mail:  admin    at etsiit.es
 */

package interfaces.farmacia;

import GestorCitas.LineaReceta;
import GestorCitas.Receta;
import GestorFarmacia.Medicamento;
import clinica.ClinicaView;
import clinica.Interfaces;
import clinica.Interfaz;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author frpp
 */
public class verRecetaDetallado extends javax.swing.JPanel {
    private TableModel modelo;
    private ClinicaView app;
    private Receta receta_1;
    private LineaReceta[] lin;
    Interfaces _anterior;


    /** Creates new customizer NewCustomizer */
    public verRecetaDetallado(ClinicaView a) {
        app = a;
        initComponents();


        jTable1.setRowHeight(32);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    //Esta interfaz es llamada SUPUESTAMENTE Y DEBERIA desde la otra de VERRECETAS..en fin..no comment!
    public void rellenaDatos(Receta receta , Interfaces ant) throws SQLException, IOException, ClassNotFoundException, ParseException {
        _anterior = ant;
        limpiarTabla();
        DateFormat fecha_formato = new SimpleDateFormat("dd/MM/yyyy");
        receta_1 = receta;
        //Cojo todos los lotes de mediamentos.
        ArrayList <LineaReceta> solucion = new ArrayList<LineaReceta>();
        lin = LineaReceta.get(receta.getIdReceta(), null, null, null, null, null, null, null);
        

        if(lin != null){
            String[] columnNames = {"Linea receta","Medicamento","Posología","Fecha Inicio","Número de tomas","¿Cancelado?"};
            Object[][] data = new Object[lin.length][];
            for(int i = 0 ;i < lin.length;i++){
                String estado = "NO";
                if(lin[i].getEstaCancelada()) estado = "SI";
                data[i] = new Object[6];
                data[i][0] = lin[i].getIdLinea();
                data[i][1] = Medicamento.get(lin[i].getIdMedicamento()).getNombreMedicamento();
                data[i][2] = "Cada "+lin[i].getCada()+" "+lin[i].getTiempoCada();
                data[i][3] = fecha_formato.format(lin[i].getFechaInicio());
                data[i][4] = lin[i].getNumTomas();
                data[i][5] = estado;
            }
            
            modelo = new DefaultTableModel(data, columnNames);
            jTable1.setModel( modelo);
                       jTable1.addMouseListener(new MouseAdapter()
   {
      @Override
      public void mouseClicked(MouseEvent e)
      {
            int fila = jTable1.getSelectedRow();
            if(fila >= 0){
                observaciones.setText(lin[fila].getObservaciones());
                juicio.setText(receta_1.getJuicioDiagnosticoReceta());
            }
      }
   });

        }
 
    }
   private void limpiarTabla() {
       Interfaz.limpiarTabla(jTable1);
       observaciones.setText("Seleccione una linea de receta");
       juicio.setText("Seleccione una linea de receta");
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        observaciones = new javax.swing.JTextPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        juicio = new javax.swing.JTextPane();

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(verRecetaDetallado.class, this);
        jButton2.setAction(actionMap.get("aceptar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(verRecetaDetallado.class);
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setLabel(resourceMap.getString("jButton2.label")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jTabbedPane1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jScrollPane1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setFont(Interfaz.setFontSize());
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Linea receta", "Medicamento", "Posología", "Fecha Inicio", "Número de tomas", "¿Cancelado?"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jTabbedPane1.addTab("Linea de receta", jScrollPane1);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        observaciones.setFont(Interfaz.setFontSize());
        observaciones.setName("observaciones"); // NOI18N
        jScrollPane3.setViewportView(observaciones);

        jTabbedPane1.addTab("Observaciones", jScrollPane3);

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        juicio.setFont(Interfaz.setFontSize());
        juicio.setName("juicio"); // NOI18N
        jScrollPane4.setViewportView(juicio);

        jTabbedPane1.addTab("Juicio diagnóstico", jScrollPane4);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 758, Short.MAX_VALUE)
                    .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    @Action
    public void aceptar(){
       app.volver(_anterior);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextPane juicio;
    private javax.swing.JTextPane observaciones;
    // End of variables declaration//GEN-END:variables

}


